package com.springlearn.springsourcecode.chap3;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wtcode
 * @Description
 * @date 2025/3/27 - 15:46
 */
public class TestTemplateMethod {

    public static void main(String[] args) {
        MyBeanFactory beanFactory = new MyBeanFactory();
        beanFactory.addBeanPostProcessor(bean -> System.out.println("解析@Autowired"));
        beanFactory.addBeanPostProcessor(bean -> System.out.println("解析@Resource"));
        beanFactory.getBean();
    }


    static class MyBeanFactory{
        public Object getBean(){
            Object bean = new Object();
            System.out.println("构造" + bean);
            System.out.println("依赖注入" + bean);
            for (BeanPostProcessor processor : processors) {
                processor.inject(bean);
            }
            System.out.println("初始化" + bean);
            return bean;
        }

        private List<BeanPostProcessor> processors = new ArrayList<>();

        public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor){
            processors.add(beanPostProcessor);
        }

    }

    static interface BeanPostProcessor{
        public void inject(Object bean); // 对依赖注入阶段的拓展
    }
}
